$(function(){
	
	
	$("#bg-color").colorpicker();
	/*
	 * 初始化编辑器
	 */
	var newsEditor = CKEDITOR.replace( 'editor',{
		language: 'zh-cn',
		toolbar : 'Basic',
		height: 480,
		uiColor : '#FFFFFF',
		extraPlugins : 'uploadimage',
		uploadUrl : '/base/upload/uploadFile',
		filebrowserUploadUrl : '/base/upload/uploadFile',
		toolbarGroups : [
			{ name: 'document', groups: [ 'mode', 'document', 'doctools' ] },
			{ name: 'clipboard', groups: [ 'clipboard', 'undo' ] },
			{ name: 'editing', groups: [ 'find', 'selection', 'spellchecker', 'editing' ] },
			{ name: 'forms', groups: [ 'forms' ] },
			{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] },
			{ name: 'paragraph', groups: [ 'list', 'indent', 'blocks', 'align', 'bidi', 'paragraph' ] },
			{ name: 'links', groups: [ 'links' ] },
			{ name: 'insert', groups: [ 'insert' ] },
			{ name: 'styles', groups: [ 'styles' ] },
			{ name: 'colors', groups: [ 'colors' ] },
			{ name: 'tools', groups: [ 'tools' ] },
			{ name: 'others', groups: [ 'others' ] },
			{ name: 'about', groups: [ 'about' ] }
		],
		removeButtons : 'Save,NewPage,Templates,Cut,Copy,Paste,Find,Replace,Scayt,Form,Checkbox,Radio,TextField,Textarea,Select,Button,ImageButton,HiddenField,Flash,Iframe,About,Image'
	});
	
	newsEditor.on( 'fileUploadRequest', function( evt ) {
		var fileLoader = evt.data.fileLoader,
		formData = new FormData(),
		xhr = fileLoader.xhr;
		xhr.open( 'POST', fileLoader.uploadUrl, true );
		formData.append( 'file', fileLoader.file, fileLoader.fileName );
		fileLoader.xhr.send( formData );
		evt.stop();
	}, null, null, 6 );

	newsEditor.on( 'fileUploadResponse', function( evt ) {
		var resData = JSON.parse(evt.data.fileLoader.xhr.response);
		evt.stop();
		// Ger XHR and response.
		var data = evt.data;
		if ( resData.errorCode == 1 ) {
			// Error occurred during upload.
			data.message = resData.msg;
			evt.cancel();
		} else {
			data.message = resData.msg;
			data.url = resData.data;
		}
	});
		
	$("#news-save-btn").on("click", function() {
		var $curr = $(this);
		var $categoryId = $curr.closest(".row").attr("category-id");
		var $itemId = $curr.closest(".row").attr("item-id");
		var $style = {"background-color":$("input[name='background_color']").val()};
		var $styleAttrs = JSON.stringify($style);
		var $content = newsEditor.getData();
		cAjax({
			url: '/web/category/editItem',
			type:'post',
			data: {
				id:$itemId,
				categoryId:$categoryId,
				styleAttrs:$styleAttrs,
				content:$content
			},
			success:function(obj){
				greeting({msg:"保存成功"})
			},
		});
	});
	
	var $styleJson = $("[name='background_color']").data("style-json");
	if($styleJson!=''&&$styleJson!=undefined&&$styleJson!=null){
		$("[name='background_color']").val($styleJson['background-color']);
	}
});
